package Test;

import entity.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * Created by Chen-D.W on 2017/8/22.
 * 关注java养成记，跟我一起学java Web
 */
public class Test {
    public static void main(String[] args) {
        //读取mybatis的配置文件
        InputStream in=
                Test.class.getResourceAsStream("/mybatis.xml");
        //根据mybatis配置文件创建sqlsession工厂
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        //获取一个sqlsession
        SqlSession session= factory.openSession();
        //sqlsession包含执行sql的方法，通过映射获取mapper中的接口方法
        UserMapper userMapper=session.getMapper(UserMapper.class);
        //使用mapper中的方法，获取用户列表
        List<User> userList= userMapper.selectUserList();
        for (User user:userList//遍历用户
             ) {
            System.out.println("姓名:"+user.getName()+
                    "       年龄:"+user.getAge()+"      性别："+
                    user.getSex());
        }
        session.close();
    }
}
